import {Component} from "@angular/core";
import {Events, IonicPage, NavController, NavParams, ToastController} from "ionic-angular";
import {WebCastPlayBackListProvider} from "../../../providers/webcast/webcast-playback-list/webcast-playback-list";
import {
    PlayBackFilterModel,
    VideoType
} from "../../../model/pages/webcast/webcast-playback-filter/webcast-playback-filter.model";
import {DateService} from '../../../services/common/date.service';

import moment from "moment";
import {copy} from "@ionic/app-scripts";

@IonicPage()
@Component({
    selector: 'playback-filter',
    templateUrl: 'webcast-playback-filter.html',
    providers: [
        WebCastPlayBackListProvider,
        DateService
    ]
})
export class WebCastPlayBackFilterPage {

    private pickerOption = {
        cssClass: 'webcast-playback-filter_picker'
    };
    //筛选条件Model
    private playbackFil: PlayBackFilterModel = new PlayBackFilterModel();
    //视频类型列表
    private typeList: VideoType[] = [];
    //是否禁用查询按钮
    private disabled: boolean = false;
    private canSubmit: boolean = true;
    //时间范围控制
    private filterRange = {
        min: '1900-01-01',
        max: '2100-01-01'
    };
    private allItem = {
     name:'全部',
     id:0
    };
    // 年度列表
    private yearList:any[]=[];

    constructor(private events: Events,
                private playbackPro: WebCastPlayBackListProvider,
                private navCtrl: NavController,
                public navParams: NavParams,
                private toastCtrl: ToastController,
                public dateService:DateService
                ) {
        this.playbackFil.starttime =this.unixResolve(new Date().getTime());
        this.yearList=this.dateService.getYearList(2010);
    }

    ionViewDidLoad() {
        this.playbackFil = this.navParams.data;
        this.getAllPlayType();
    }

    getAllPlayType() {
        this.playbackPro.getVideoTypes().subscribe((res: Array<any>) => {
            this.typeList=res;
        });
    }

    changeFilter(val) {
        this.playbackFil.type=val;
    }


    cleanTime() {
        this.playbackFil.starttime = this.playbackFil.endtime = null;
    }

    resetForm() {
        this.playbackFil = new PlayBackFilterModel();
        this.playbackFil.type=0;
    }

    doSearch() {
        this.canSubmit && this.navCtrl.pop().then(() => {
            this.events.publish('filter-events', this.playbackFil);
        })
    }
    unixResolve(time) {
        if (time) {
            return moment.unix(time).format('YYYY/MM/DD')
        } else {
            return '';
        }
    }
    validateForm() {
        if (this.playbackFil.starttime && this.playbackFil.endtime
            && new Date(this.playbackFil.starttime) > new Date(this.playbackFil.endtime)) {
            this.toastCtrl.create({
                duration: 1500,
                position: 'middle',
                message: '直播时间输入有误'
            }).present();
            this.canSubmit = false;
        } else {
            this.canSubmit = true;
        }
    }
}
